//
//  LS_TableViewBindingHelper.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/9.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_TableViewBindingHelper.h"
#import "LS_Reactiveview.h"
@interface LS_TableViewBindingHelper()<UITableViewDelegate,UITableViewDataSource>
/**
 *  tableView
 */
@property(nonatomic,strong)UITableView *tableView;
/**
 *  数据源
 */
@property (strong, nonatomic) NSArray *data;
/**
 *  selection
 */
@property (strong, nonatomic) RACCommand *didSelectionCommand;
/**
 *  cell重用标识
 */
@property (copy, nonatomic) NSString *cellIdentifier;
/**
 *  viewModel
 */
@property (strong, nonatomic) id viewModel;
@end
@implementation LS_TableViewBindingHelper
-(instancetype)initWithTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source selectionCommand:(RACCommand *)didSelectionCommand withCellType:(NSDictionary *)CellTypeDic withViewModel:(id)viewModel
{
    if (self = [super init]) {
    
    _tableView = tableView;
    _data = [NSArray array];
    _didSelectionCommand = didSelectionCommand;
    _viewModel = viewModel;
    
    @weakify(self)
    [source subscribeNext:^(id x) {
        @strongify(self)
        self.data = x;
        [self.tableView reloadData];
    }];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    NSString *cellType = CellTypeDic[@"cellType"];//判断类型是xib 还是纯代码
    _cellIdentifier = CellTypeDic[@"cellName"];
    Class cell = NSClassFromString(_cellIdentifier);
    [_tableView registerClass:cell forCellReuseIdentifier:_cellIdentifier];
        
    }
    return self;
}
-(void)dealloc
{
    self.delegate = nil;
}
+(instancetype)bindingHelperForTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source selectCommand:(RACCommand *)didSelctionCommad templateCell:(NSString *)templateCell withViewModel:(id)viewModel
{
    NSDictionary *cellDict = @{@"cellType":@"codeType",@"cellName":templateCell};
    return [[LS_TableViewBindingHelper alloc]initWithTableView:tableView sourceSignal:source selectionCommand:didSelctionCommad withCellType:cellDict withViewModel:viewModel];
}
#pragma mark -
#pragma mark -- delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.tableView.mj_footer.hidden = (self.data.count == 0) ? YES : NO;
    return self.data.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id<LS_Reactiveview >  cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier];
    [cell bindViewModel:self.viewModel withParams:@{DataIndex:@(indexPath.row)}];
    return (UITableViewCell *)cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.didSelectionCommand execute:self.data[indexPath.row]];
    if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}
#pragma mark = UITableViewDelegate forwarding

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([self.delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
        [self.delegate scrollViewDidScroll:scrollView];
    }
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    if ([self.delegate respondsToSelector:aSelector]) {
        return YES;
    }
    return [super respondsToSelector:aSelector];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    if ([self.delegate respondsToSelector:aSelector]) {
        return self.delegate;
    }
    return [super forwardingTargetForSelector:aSelector];
}

@end
